<?php 

defined('BASEPATH') OR exit('No direct script access allowed');

class login extends CI_Controller {

	function __construct(){
		parent:: __construct();
		$this->load->library('form_validation');
		$this->load->model("mod_book");
	}
	function index(){
		if($this->session->userdata("admin_loggedIn")){
			redirect("admin/viewBook");
		}else{ 
			$this->form_validation->set_rules('password', 'Password', 'trim|required');
			$this->form_validation->set_rules('username', 'User Name', 'trim|required');
			if($this->form_validation->run() == FALSE){
				$this->load->view("login");
			}else{
				$username = $this->input->post('username');
				$password = $this->input->post('password');
				$remember = $this->input->post('remember');
				$login = $this->mod_book->getAdmin($username,MD5($password));
				if($login){
					$ses_arr = array(
						"first_name"=>$login->firstname,
						"userAdmin"=>$login->firstname.' '.$login->lastname,
						"emailAdmin"=>$login->email,
						"userId"=>$login->userid,
						"admin_loggedIn"=>true
					);
					$this->session->set_userdata($ses_arr);
					redirect("admin/viewBook");
				}else{
					echo "<script>alert('User Name or Password invalid!');</script>"; 
					$this->load->view("login");
				}
			}
		}


	}
}